InetAddr : Internet address operating
This module used for network address operation, such as validity verification and mask prefix conversion.
User can use the following code to import the inetaddr
module.
var inetaddr = require('inetaddr');
Support
The following shows inetaddr
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
inetaddr.domain | ● | ● |
inetaddr.isIPv4 | ● | ● |
inetaddr.isIPv6 | ● | ● |
inetaddr.addrValid | ● | ● |
inetaddr.maskValid | ● | ● |
inetaddr.toPrefix | ● | ● |
inetaddr.fromPrefix | ● | ● |
inetaddr.compare | ● | ● |
inetaddr.subnet | ● | ● |
inetaddr.broadcast | ● | ● |
inetaddr.aton | ● | ● |
inetaddr.ntoa | ● | ● |
inetaddr.htonl | ● | ● |
inetaddr.htons | ● | ● |
inetaddr.ntohl | ● | ● |
inetaddr.ntohs | ● | ● |
Inetaddr Module
inetaddr.domain(addr)
addr
{String} Address string.- Returns: {Integer}
inetaddr.AF_INET
orinetaddr.AF_INET6
.
Get the domain value corresponding to the IP address. This function is available in EdgerOS 1.9.5 and later.
inetaddr.isIPv4(addr)
addr
{String} Address string.- Returns: {Boolean} Whether the address is IPv4.
Check if the input string is a valid IPv4 address, valid return true, otherwise false.
Example
inetaddr.isIPv4('ABC');
// return false
inetaddr.isIPv4('192.168.0.1');
// return true
inetaddr.isIPv6(addr)
addr
{String} Address string.- Returns: {Boolean} Whether the address is IPv6.
Check if the input string is a valid IPv6 address, valid return true, otherwise false.
Example
inetaddr.isIPv6('192.168.0.1');
// return false
inetaddr.isIPv6('::1');
// return true
inetaddr.addrValid(addr)
addr
{String} Address string.- Returns: {Boolean} Whether the address is valid.
Check if the input string is a valid IPv4 or IPv6 address, valid return true, otherwise false.
Example
inetaddr.addrValid('aaaa');
// return false
inetaddr.addrValid('192.168.0.1');
// return true
inetaddr.addrValid('::1');
// return true
inetaddr.maskValid(netmask)
netmask
{String} Netmask string.- Returns: {Boolean} Whether the netmask is valid.
Check if the input string is a valid IPv4 or IPv6 netmask, valid return true, otherwise false.
Example
inetaddr.maskValid('aaaa');
// return false
inetaddr.maskValid('192.168.0');
// return false
inetaddr.maskValid('192.168.0.1');
// return false
inetaddr.maskValid('255.255.0.0');
// return true
inetaddr.maskValid('255.255.128.0');
// return true
inetaddr.toPrefix(netmask)
netmask
{String} Netmask string.- Returns: {Integer} Netmask prefix length.
Calculate the netmask
prefix length, return negative on error.
Example
inetaddr.toPrefix('255.255.255.0');
// return 24
inetaddr.toPrefix('255.255.0.0');
// return 16
inetaddr.toPrefix('ffff::0');
// return 16
inetaddr.fromPrefix(prefix[, isIPv6])
prefix
{Integer} Netmask prefix length.isIPv6
{Boolean} Whether to return an IPv6 netmask. default: false.- Returns: {String} Netmask string.
Generates a netmask string with the specified prefix
length. The valid length of the IPv4 prefix is 0 ~ 32, and the IPv6 is 0 ~ 128.
Example
inetaddr.fromPrefix(24);
// return '255.255.255.0'
inetaddr.fromPrefix(16);
// return '255.255.0.0'
inetaddr.fromPrefix(16, true);
// return 'ffff::'
inetaddr.compare(addr1, addr2)
addr1
{String} IP addresss 1.addr2
{String} IP addresss 2.- Returns {Integer} Comparing results.
Compare two IP addresses (IPv4 Only), if addr1
> addr2
returns 1
, if addr1
< addr2
returns -1
and if addr1
=== addr2
returns 0
.
Example
inetaddr.compare('10.0.0.1', '10.0.0.2'); // -1
inetaddr.compare('10.0.0.3', '10.0.0.2'); // 1
inetaddr.subnet(addr, netmask)
addr
{String} IP addresss.netmask
{String} Netmask.- Returns {String} Subnet address.
Get subnet address by IP address and netmask.
Example
var subnet = inetaddr.subnet('192.168.0.1', '255.255.255.0');
// 192.168.0.0
inetaddr.broadcast(addr, netmask)
addr
{String} IP addresss.netmask
{String} Netmask.- Returns {String} Broadcast address.
Get broadcast address by IP address and netmask. This function is added in EdgerOS 1.6.0 and later versions.
Example
var subnet = inetaddr.broadcast('192.168.0.1', '255.255.255.0');
// 192.168.0.255
inetaddr.aton(addr)
addr
{String} IP address string.- Returns: {Integer | Array} IP address in network byte order.
Convert string IP address to network byte order address. EdgerOS 1.6.0 and later versions support.
Example
var addr = inetaddr.aton('1.2.3.4');
// 0x04030201 (little endian)
var addr = inetaddr.aton('1:2::3:4');
// [0x2000100, 0, 0, 0x4000300]
inetaddr.ntoa(addr)
addr
{Integer | Array} IP address in network byte order.- Returns: {String} IP address string.
Convert network byte order address to string IP address. EdgerOS 1.6.0 and later versions support.
Example
var addr = inetaddr.ntoa(0x04030201);
// '1.2.3.4'
var addr = inetaddr.ntoa([0x2000100, 0, 0, 0x4000300]);
// '1:2::3:4'
inetaddr.htonl(addr)
addr
{Integer} 32-bit integer number.- Returns: {Integer} 32-bit integer number.
Host to network sequence 32-bit integer conversion. EdgerOS 1.6.0 and later versions support.
Example
console.log(inetaddr.htonl(0x01020304)); // 0x04030201 (little endian) 0x01020304 (big endian)
inetaddr.htons(port)
port
{Integer} 16-bit integer number.- Returns: {Integer} 16-bit integer number.
Host to network sequence 16-bit integer conversion. EdgerOS 1.6.0 and later versions support.
Example
console.log(inetaddr.htonl(0x0102)); // 0x0201 (little endian) 0x0102 (big endian)
inetaddr.ntohl(addr)
addr
{Integer} 32-bit integer number.- Returns: {Integer} 32-bit integer number.
Network sequence to host 32-bit integer conversion. same as inetaddr.htonl()
EdgerOS 1.6.0 and later versions support.
inetaddr.ntohs(port)
port
{Integer} 16-bit integer number.- Returns: {Integer} 16-bit integer number.
Network sequence to host 16-bit integer conversion. same as inetaddr.htons()
EdgerOS 1.6.0 and later versions support.
Inetaddr Module Constant
inetaddr.AF_INET
- {Integer}
2
inetaddr.AF_INET6
- {Integer}
10
inetaddr.INADDR_NONE
- {String}
'255.255.255.255'
inetaddr.INADDR_LOOPBACK
- {String}
'127.0.0.1'
inetaddr.INADDR_ANY
- {String}
'0.0.0.0'
inetaddr.INADDR_BROADCAST
- {String}
'255.255.255.255'
inetaddr.IN6ADDR_ANY
- {String}
'::'
inetaddr.IN6ADDR_LOOPBACK
- {String}
'::1'
inetaddr.IN6ADDR_NODELOCAL_ALLNODES
- {String}
'ff01::1'
inetaddr.IN6ADDR_LINKLOCAL_ALLNODES
- {String}
'ff02::1'
inetaddr.IN6ADDR_LINKLOCAL_ALLROUTERS
- {String}
'ff01::2'